
#include <BMouseEvent>
#include <BImage>
#include <BPainter>
#include <BViewer>
#include <BThread>
#include <BSurface>
#include <BSphere>
#include <BCamera>
#include <BViewer>
#include <BScene>
#include <BProgram>
#include <BMaterial>
#include <BHBoxLayout>
#include <BApplication>
#include <BSphereHandler>
#include <BMainWindow>
#include <BDialog>

using namespace BWE;

class TrayDialog : public BDialog
{
public:
	TrayDialog()
	{
		_inside = false;
		_pressed = false;
		_image = new BImage("data/image/Multimedia_Icons_009.png");
	}
	~TrayDialog()
	{

	}

protected:
	virtual void paintEvent(const BEvent& event)
	{
		BRect rect = this->size();
		BPainter painter(this);
		painter.setLineWidth(3);
		if (_inside)
			painter.setColor(200, 0, 200);
		else
			painter.setColor(200, 0, 0);
		painter.setLineSmooth(true);
		painter.drawEllipse(rect.center(), rect.width() * 0.4);
		//painter.setTexture(_image);
		painter.fillEllipse(rect.center(), rect.width() * 0.3);

		painter.setColor(0, 255, 0, 128);
		painter.fillRect(rect.left(), rect.top(), rect.width() / 2, rect.height());
	}

	virtual void mouseEnterEvent(const BMouseEvent& mouseEvent)
	{
		_inside = false;
		_pressed = false;
	}
	virtual void mouseLeaveEvent(const BMouseEvent& mouseEvent)
	{
		_inside = false;
		_pressed = false;
		this->fresh();
	}

	virtual void mousePressEvent(const BMouseEvent& mouseEvent)
	{
		if (mouseEvent.button() == Button_Left)
		{
			_pressed = true;
			_pressedPos = mouseEvent.pos();
		}
	}
	virtual void mouseReleaseEvent(const BMouseEvent& mouseEvent)
	{
		_pressed = false;
	}

	virtual void mouseMoveEvent(const BMouseEvent& mouseEvent)
	{
		BPoint pos = mouseEvent.pos();
		if (_pressed && _inside)
		{
			BPoint offset = pos - _pressedPos;
			this->move(offset);
			return;
		}
		BRect rect = this->size();
		float dist = pos.distance(rect.center());
		float radius = rect.width() * 0.4;
		bool inside = dist < radius;
		if (_inside != inside)
		{
			_inside = inside;
			this->fresh();
			return;
		}
	}

private:
	bool		_inside;
	bool		_pressed;
	BPoint		_pressedPos;
	BImage*		_image;
};

int main(int argc, char** argv)
{
	BApplication app;
	
	TrayDialog dialog;
	dialog.setSize(500, 400);
	dialog.setMargins(2, 2, 2, 2);
	dialog.show();

	return app.execute();
}
